package com.atguigu.tingshu.listenner;

import com.atguigu.tingshu.model.user.UserInfo;
import io.xzxj.canal.core.annotation.CanalListener;
import io.xzxj.canal.core.listener.EntryListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Set;

/**
 * @author 饭勺
 * @description 监听到变更业务处理类-用户信息
 * @createDate 2025/10/15 17:20
 */
@Slf4j
@CanalListener(destination = "tingshuTopic",schemaName = "tingshu_user",tableName = "user_info")
public class UserListener implements EntryListener<UserInfo> {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 监听用户表更新回调方法
     * @param before
     * @param after
     * @param fields
     */
    @Override
    public void update(UserInfo before, UserInfo after, Set<String> fields) {
        log.info("[cdc]监听到变更数据");
        String redisKey = "user:info:"+after.getId();
        redisTemplate.delete(redisKey);
    }
}